package com.tanhua.server.controller;

import com.tanhua.server.service.UsersService;
import com.tanhua.server.vo.CountsVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.SettingsVo;
import com.tanhua.server.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: JinboYang
 * @Description:
 * @Date: Created in 下午 19:14 2021/1/14
 * @ModifiedBy:
 */
@RestController
@RequestMapping("users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    /**
     * @Description: 用户资料读取
     * @Param:[userID, huanxinID]
     * @return:org.springframework.http.ResponseEntity<com.tanhua.server.vo.UserInfoVo>
     * @Author:JinboYang
     * @Date:2021/1/14
     */
    @GetMapping
    public ResponseEntity<UserInfoVo> queryUserInfo(@RequestParam(value = "userID", required = false) String userID,
                                                    @RequestParam(value = "huanxinID", required = false) String huanxinID) {

        try {
            //第一次接受到环信的信息
            UserInfoVo userInfoVo = this.usersService.queryUserInfo(userID, huanxinID);
            if (userInfoVo != null) {
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Description: 更新用户信息
     * @Param:[userInfoVo]
     * @return:org.springframework.http.ResponseEntity<java.lang.Void>
     * @Author:JinboYang
     * @Date:2021/1/14
     */
    @PutMapping
    public ResponseEntity<Void> updateUserInfo(@RequestBody UserInfoVo userInfoVo) {

        try {
            Boolean bool = this.usersService.updateUserInfo(userInfoVo);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Description: 相互喜欢数, 粉丝数, 喜欢数
     * @Param:[]
     * @return:org.springframework.http.ResponseEntity<com.tanhua.server.vo.CountsVo>
     * @Author:JinboYang
     * @Date:2021/1/16
     */
    @GetMapping("counts")
    public ResponseEntity<CountsVo> queryCounts() {
        try {
            CountsVo countsVo = this.usersService.queryCounts();
            if (null != countsVo) {
                return ResponseEntity.ok(countsVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Description: 互相关注、我关注、粉丝、谁看过我 - 翻页列表
     * @Param:[type, page, pageSize, nickname]
     * @return:org.springframework.http.ResponseEntity<com.tanhua.server.vo.PageResult>
     * @Author:JinboYang
     * @Date:2021/1/16
     */
    @GetMapping("friends/{type}")
    public ResponseEntity<PageResult> queryLikeList(@PathVariable("type") String type,
                                                    @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                    @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "nickname", required = false) String nickname) {
        try {
            page = Math.max(1, page);
            PageResult pageResult = this.usersService.queryLikeList(Integer.valueOf(type), page, pageSize, nickname);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Description: 取消喜欢
     * @Param:[userId]
     * @return:org.springframework.http.ResponseEntity<java.lang.Void>
     * @Author:JinboYang
     * @Date:2021/1/16
     */
    @DeleteMapping("like/{uid}")
    public ResponseEntity<Void> disLike(@PathVariable("uid") Long userId) {
        try {
            this.usersService.disLike(userId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Description: 关注粉丝
     * @Param:[userId]
     * @return:org.springframework.http.ResponseEntity<java.lang.Void>
     * @Author:JinboYang
     * @Date:2021/1/16
     */
    @PostMapping("fans/{uid}")
    public ResponseEntity<Void> likeFan(@PathVariable("uid") Long userId) {
        try {
            this.usersService.likeFan(userId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
       * @Description: 查询配置
       * @Param:[]
       * @return:org.springframework.http.ResponseEntity<com.tanhua.server.vo.SettingsVo>
       * @Author:JinboYang
       * @Date:2021/1/16
       */
    @GetMapping("settings")
    public ResponseEntity<SettingsVo> querySettings() {
        try {
            SettingsVo settingsVo = this.usersService.querySettings();
            if (null != settingsVo) {
                return ResponseEntity.ok(settingsVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
       * @Description: 设置陌生人问题
       * @Param:[param]
       * @return:org.springframework.http.ResponseEntity<java.lang.Void>
       * @Author:JinboYang
       * @Date:2021/1/16
       */
    @PostMapping("questions")
    public ResponseEntity<Void> saveQuestions(@RequestBody Map<String, String> param) {
        try {
            String content = param.get("content");
            this.usersService.saveQuestions(content);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
